package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.clue.FalseCluePoolDO;
import com.qk.dto.clue.*;
import com.qk.entity.Clue;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    ClueService clueService;

    //线索池
    @GetMapping("/pool")
    public Result falseCluePool(FalseCluePoolDTO dto){
        PageResult<FalseCluePoolDO> page = clueService.falseCluePool(dto);
        return Result.success(page);
    }

    //伪线索
    @PutMapping("/false/{id}")
    public Result toFalseClue(@PathVariable Integer id, @RequestBody FalseClueDTO falseClueDTO){
        clueService.toFalseClue(id, falseClueDTO);
        return Result.success();
    }

    //转商机
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        clueService.toBusiness(id);
        return Result.success();
    }

    //跟进线索
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO updateClueInfoDTO){
        clueService.updateClueInfoById(updateClueInfoDTO);
        return Result.success();
    }

    //参数回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable(name = "id") Integer id){
        return Result.success(clueService.getClueById(id));
    }

    //分配线索
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateClueById(@PathVariable Integer clueId, @PathVariable Integer userId){
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping
    public Result listClues(ClueListDTO dto){
        return Result.success(clueService.listClues(dto));
    }

    @PostMapping
    public Result addClue(@RequestBody ClueDTO clueDTO) {
        clueService.addClue(clueDTO);
        return Result.success();
    }
}